<template>
  <page-layout>
    <a-card :bordered="false">
      <!-- 查询区域 -->
      <div>
        <a-form @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :md="6" :sm="12">
              <a-form-item label="编码" :label-col="labelCol" :wrapper-col="wrapperCol">
                <a-input v-model="queryParam.dictCode" placeholder="请输入编码" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="名称" :label-col="labelCol" :wrapper-col="wrapperCol">
                <a-input v-model="queryParam.dictName" placeholder="请输入名称" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item>
                <a-button type="primary" icon="search" @click="searchQuery()">查询</a-button>
                <a-button type="primary" icon="reload" style="margin-left: 8px" @click="searchReset()">重置</a-button>
                <a style="margin-left: 8px" @click="handleToggleSearch">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
                </a>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- table区域-begin -->
      <div>
        <vxe-grid
          id="UserList"
          ref="vxeTable"
          :height="null"
          v-bind="gridOptions"
          :row-config="{keyField:'id'}"
          v-on="listeners"
        >
          <template v-slot:toolbar_tools>
            <div >
              <a-button type="primary" icon="sync" ghost class="theme--perfect-button" @click="setAuth">设置权限</a-button>
              <a-button type="primary" icon="sync" ghost class="theme--perfect-button" @click="clearAuth">清空权限</a-button>
            </div>
          </template>
          <template #dictText="{row,column}">
            <span>{{ getDictText(row[column.field]) }}</span>
          </template>
          <template #action="{ row }">
            <template>
              <a @click="handleEdit(row)">编辑</a>
              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">
                  更多 <a-icon type="down" />
                </a>
                <a-menu slot="overlay">
                  <a-menu-item v-hasPermi="'erp:AuthList:add'">
                    <a  @click="handleAdd(row)">新增</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a href="javascript:;" @click="handleDetail(row)">详情</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(row.id)">
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </template>
          </template>
        </vxe-grid>
      </div>
      <!-- table区域-end -->
      <auth-modal ref="modalForm" @ok="modalFormOk" />
    </a-card>
  </page-layout>
</template>

<script>
import AuthModal from './modules/AuthModal'
import PageLayout from '@/layouts/PageLayout'
import VxeTableListMixin from '@/mixins/VxeTableListMixin'
import storage from 'store'
import { PERMISSIONS, ROLES } from '@/store/mutation-types'
export default {
  name: 'AuthList',
  components: {
    AuthModal,
    PageLayout
  },
  mixins: [VxeTableListMixin],
  data() {
    return {
      columns: [
        { type: 'checkbox', width: 40 },
        { type: 'seq', width: 40 },
        {
          title: '编码',
          field: 'dictCode',
          sortable: true
        },
        {
          title: '名称',
          field: 'dictName'
        },
        {
          title: '默认值',
          field: 'dictDefaultValue'
        },
        {
          title: '描述',
          field: 'dictDesc'
        },
        {
          title: '类别',
          field: 'dictType',
          slots: { default: ({ row }) => {
            return row.dictType === '1' ? '系统字典' : '业务字典'
          } }
        },
        {
          title: '操作',
          field: 'action',
          slots: { default: 'action' },
          width: 170
        }
      ],
      url: {
        list: '/sys/dict/tree/page',
        delete: '',
        deleteItem: '',
        deleteBatch: ''
      },
      dataSource: [{ dictCode: 'aaa', dictName: '我是一个测试' }],
      // 查询参数
      queryParam: {},
      toggleSearchStatus: false,
      innerTableWidth: 600,
      code: '',
      toolbarConfig: {
        slots: {
          // tools: 'toolbar_tools'
        },
        tools: [
          {
            toolRender: {
              name: 'VxeToolbarSearch',
              props: {
                fieldList: [
                  { type: 'input', value: 'dictCode', text: '编码' },
                  { type: 'input', value: 'dictName', text: '名称' }
                ]
              },
              events: { click: this.handleSuperQuery }
            }
          }
        ]
        // buttons: []
      },
      auth: {
        permi: {
          add: 'AuthList:add',
          table: 'AuthList:table'
        }
      }
    }
  },
  methods: {
    setAuth() {
      console.log(storage.get(PERMISSIONS))
      const permi = JSON.parse(storage.get(PERMISSIONS))
      if (permi.indexOf('AuthList:add') < 0) {
        permi.push('AuthList:add')
        storage.set(PERMISSIONS, JSON.stringify(permi))
        console.log('请刷新页面')
        // window.location.reload()
      }
    },
    clearAuth() {
      const permi = JSON.parse(storage.get(PERMISSIONS))
      console.log(permi)
      const index = permi.indexOf('AuthList:add')
      if (index >= 0) {
        permi.splice(index, 1)
        storage.set(PERMISSIONS, JSON.stringify(permi))
        console.log('请刷新页面')
        // window.location.reload()
      }
    }
  },
  mounted() {
    // this.initAuth()
  }
}
</script>

